package com.base.git;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.LogCommand;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;



public class GitHistoryRemoteDemo {


    // 远程仓库URL
    private static final String remoteUrl = "https://gitee.com/xuyou551/base-demo.git";
    // 用户名
    private static final String username = "179647302@qq.com";
    // 密码或访问令牌
    private static final String password = "xu123123";


    public static void main(String[] args) throws Exception {
        // 本地仓库路径
        String localPath = "D:\\java\\work\\temp";
        // 分支名称
        String branch = "refs/remotes/origin/master";
        try {
            List<String> commitHistory = getCommitHistory(localPath, branch);
            for (String commit : commitHistory) {
                System.out.println(commit);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
//            File file = new File(localPath);
//            if (file.exists()) {
//                deleteFolder(file);
//            }
        }
    }


    public static void deleteFolder(File folder) {
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    deleteFolder(file);
                }
            }
        }
        folder.delete();
    }

    public static List<String> getCommitHistory(String localPath , String branch) throws Exception {
        List<String> commitHistory = new ArrayList<>();

        try (Repository repository = openRepository(localPath)) {
            List<Ref> refs = repository.getRefDatabase().getRefs();
            for (Ref head : refs) {
                String name = head.getName();
                System.out.println(name);
                if (!StringUtils.isEmpty(branch) && !name.equals(branch)) {
                    continue;
                }
                ObjectId branchId = head.getObjectId();
                LogCommand logCommand = Git.wrap(repository).log().add(branchId);
                Iterable<RevCommit> commits = logCommand.call();
                for (RevCommit commit : commits) {
                    commitHistory.add(commit.getFullMessage());
                }
            }
        }

        return commitHistory;
    }

    private static Repository openRepository(String localPath) throws Exception {
        Git git = Git.cloneRepository()
                .setURI(remoteUrl)
                .setDirectory(new File(localPath)) // 临时目录用于存储 Git 仓库
                .setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password))
                .call();
        return git.getRepository();
    }
}